package com.huang.leetcode.tree;

import com.huang.leetcode.constant.TreeNode;

/**
 * @Author：CreateSequence
 * @Date：2020-07-24 23:25
 * @Description：对称的二叉树
 */
public class PB101 {

    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return isSymmetric(root.left, root.right);
    }

    public boolean isSymmetric(TreeNode left, TreeNode right) {
        //左右节点都为空，即抵达到叶子节点，并且两叶子节点相同
        if (right == null && left == null) {
            return true;
        }

        //抵达叶子结点但是有一边没有，即不对称
        if (right == null || left == null) {
            return false;
        }

        //两边值不相等
        if (right.val != left.val) {
            return false;
        }

        //向两侧子树递归
        return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
    }

}
